package com.example.mymvvmdemo.net.model;


import androidx.annotation.NonNull;


import com.example.mymvvmdemo.api.API;
import com.example.mymvvmdemo.retrofit.RetrofitManager;
import com.example.mymvvmdemo.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * 普通Post请求统一model
 */
public class PostModel extends BaseModel {

    private IModel.PostModel postModel;

    public PostModel(IModel.PostModel postModel) {
        this.postModel = postModel;
    }

    public void startLoadPost(String url, String auth_token, HashMap<String, String> hashMap, String type) {

        RetrofitManager.getInstance().getRetrofit().create(API.class).loadPost(url, auth_token, hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
//                        LogUtils.d("开始请求...");
                    }

                    @Override
                    public void onNext(@NonNull ResponseBody response) {
//                        LogUtils.d("数据回调:" + response.toString());


                        try {
                            String result = response.string();
                            //判断请求网络是否异常
                            if (isNetWorkError(result)) {
                                postModel.loadPostNetWorkError(result);
                                return;
                            }

                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            //检查是否账号异常（被挤或者token异常)
//                            boolean isError = CheckOutUtils.checkErrorCode(code);
//                            if (isError) {
//                                getModel.loadGetAccountError(code);
//                            } else {
                            postModel.loadPostSuccess(result, type, code);
//                            }

                        } catch (Exception e) {
                            LogUtils.e(e.getMessage());
                            postModel.loadPostFail(e.getMessage(), type);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //判断请求网络是否异常
                        if (isNetWorkError(e.getMessage())) {
                            postModel.loadPostNetWorkError(e.getMessage());
                            return;
                        }
                        postModel.loadPostFail(e.getMessage(), type);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
